#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from RenrenAPI.renrenAPI import RenrenAPI
from RenrenAPI import oauth
import unittest
import simplejson as json
import webbrowser


class OAuthTest(unittest.TestCase):
    """
    Test for the OAuth operation
    Get Requset Token
    Get Access Token
    """

    consumer_key = ''
    consumer_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 32)
        self.assertEqual(len(self.consumer_secret), 32)

    def testUsername(self):
        """test for loading username"""
        path = os.path.join(os.path.dirname(__file__), 'user.txt')
        f = open(path, 'r')
        userStr = f.read()
        f.close()
        user = json.loads(userStr)
        self.username = user['username']
        self.password = user['password']
        self.assertTrue(len(self.username) > 1)
        self.assertTrue(len(self.password) > 1)


    def testAccessToken(self):
        """test for getting access token"""
        self.testConsumer()
        self.testUsername()
        consumerToken = oauth.OAuthToken(self.consumer_key, \
                                         self.consumer_secret)
        testAPI = RenrenAPI(consumerToken)
        data = {}
        data['grant_type'] = 'password'
        data['username'] = self.username
        data['password'] = self.password
        data['client_id'] = self.consumer_key
        data['client_secret'] = self.consumer_secret
        access_token = testAPI.get_access_token(data)
        print access_token
        self.assertTrue(len(access_token['access_token']) > 0)

if __name__ == '__main__':
    unittest.main()
